package com.huayi.website.controller;

import com.huayi.website.entity.EnumCompany;
import com.huayi.website.entity.EnumLanguage;
import com.huayi.website.entity.EnumPages;
import com.huayi.website.service.BannerService;
import com.huayi.website.service.PagesService;
import com.huayi.website.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ProductController {
    @Autowired
    private PagesService pagesService;
    @Autowired
    private BannerService bannerService;
    @Autowired
    private ProductService productService;

    @RequestMapping("/{language}/yunshang/products")
    public String productList(HttpServletRequest request, @PathVariable String language,  Model model){
        EnumLanguage languageEnum = EnumLanguage.getEnum(language);
        EnumCompany company = EnumCompany.YunShange;
        model.addAttribute("language",language);

        var banners = bannerService.getAllList(EnumPages.About_Yun);
        model.addAttribute("banners",banners);

        Integer pageIndex = 1, pageSize = 12;
        if (!StringUtils.isEmpty(request.getParameter("page"))) {
            pageIndex = Integer.parseInt(request.getParameter("page"));
        }
        var result = productService.getAllList(pageIndex,pageSize,"",null,company.getIndex());
        model.addAttribute("list",result.getList());
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("totalSize", result.getTotalSize());


        model.addAttribute("menuId","profile");

        //获取页面信息，设置title
        var page = pagesService.getById(EnumPages.About_Yun);
        if (languageEnum == EnumLanguage.CHINESE) {
            model.addAttribute("layout", "layout_zh");
            model.addAttribute("pageTitle", page.getTitleCn());
        }
        else {
            model.addAttribute("pageTitle", page.getTitleEn());
            model.addAttribute("layout", "layout");
        }
        return "product";
    }

    @RequestMapping("/{language}/jinlin/products")
    public String productList1(HttpServletRequest request, @PathVariable String language,  Model model){
        EnumLanguage languageEnum = EnumLanguage.getEnum(language);
        EnumCompany company = EnumCompany.JinLin;
        model.addAttribute("language",language);

        var banners = bannerService.getAllList(EnumPages.About_JinLin);
        model.addAttribute("banners",banners);

        Integer pageIndex = 1, pageSize = 12;
        if (!StringUtils.isEmpty(request.getParameter("page"))) {
            pageIndex = Integer.parseInt(request.getParameter("page"));
        }
        var result = productService.getAllList(pageIndex,pageSize,"",null,company.getIndex());
        model.addAttribute("list",result.getList());
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("totalSize", result.getTotalSize());



        model.addAttribute("menuId","profile");

        //获取页面信息，设置title
        var page = pagesService.getById(EnumPages.About_JinLin);
        if (languageEnum == EnumLanguage.CHINESE) {
            model.addAttribute("layout", "layout_zh");
            model.addAttribute("pageTitle", page.getTitleCn());
        }
        else {
            model.addAttribute("pageTitle", page.getTitleEn());
            model.addAttribute("layout", "layout");
        }
        return "product";
    }

    /**
     * 产品详情
     * @param request
     * @param language
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("/{language}/productDetail/{productId}")
    public String home(HttpServletRequest request,  @PathVariable String language,@PathVariable Integer productId, Model model){
        EnumLanguage languageEnum = EnumLanguage.getEnum(language);


        //获取商品信息
        var product = productService.getDetailById(productId);
        model.addAttribute("product", product);



        model.addAttribute("pageTitle","Product Detail");
        if (languageEnum == EnumLanguage.CHINESE) {
            model.addAttribute("productTitle",product.getProductName());
            model.addAttribute("productDesc",product.getContent());
            model.addAttribute("layout", "layout_zh");
        }
        else {
            model.addAttribute("productTitle",product.getProductEname());
            model.addAttribute("productDesc",product.geteContent());
            model.addAttribute("layout", "layout");
        }

        model.addAttribute("menuId","profile");
        return "product_detail";
    }
}
